package com.eclink.iot.base;

import com.eclink.utils.ByteUtils;
import com.eclink.utils.Log;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;


public abstract class SocketClientBase
  extends Thread
{
  protected String TAG = "";
  
  private Socket socket;
  private InputStream input_stream;
  private DataInputStream data_input_stream;
  private OutputStream output_stream;
  protected String uuid;
  protected String version;
  protected int runtime = 0;
  
  protected boolean is_running = true;
  protected boolean online_status = false;
  protected ConnectStatusChangeListener connect_status_change_listener = null;
  
  protected abstract void concat_data(byte[] paramArrayOfbyte, int paramInt);
  
  public SocketClientBase(Socket socket, ConnectStatusChangeListener listener) {
    this.socket = socket;
    this.connect_status_change_listener = listener;
  }
  
  public void release() {
    this.is_running = false;
    try {
      if (this.data_input_stream != null) {
        this.data_input_stream.close();
      }
      if (this.input_stream != null) {
        this.input_stream.close();
      }
      if (this.output_stream != null) {
        this.output_stream.close();
      }
      if (this.socket != null) {
        this.socket.close();
      }
    } catch (Exception e) {
      e.printStackTrace();
    } 
  }
  
  protected void online_status_change_notification(boolean status) {
    if (this.online_status != status && this.connect_status_change_listener != null) {
      this.connect_status_change_listener.notice(this, this.uuid, this.online_status = status);
    }
  }
  
  protected boolean write(byte... buffer) {
    try {
      this.output_stream = this.socket.getOutputStream();
      
      this.output_stream.write(buffer);
      
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      release();
      
      return false;
    } 
  }
  
  protected boolean write(byte[] buffer, int length) {
    return write(ByteUtils.subString(buffer, 0, length));
  }
  
  public void run() {
    try {
      this.input_stream = this.socket.getInputStream();
      
      this.data_input_stream = new DataInputStream(this.input_stream);
      
      while (this.is_running) {
        
        try {
          if (this.data_input_stream.available() > 0) {
            
            byte[] buffer = new byte[1024];
            int length = this.data_input_stream.read(buffer);
            
            if (length == -1) {
              Log.e(this.TAG, "client disconnected");
              
              break;
            } 
            
            concat_data(buffer, length);
          } 
          
          Thread.sleep(1L);
        } catch (Exception e) {
          e.printStackTrace();
        } 
      } 
      
      online_status_change_notification(false);
    } catch (Exception e) {
      e.printStackTrace();
    } 
  }
  
  public static interface ConnectStatusChangeListener {
    void notice(Object param1Object, String param1String, boolean param1Boolean);
  }
}


/* Location:              E:\code\work\eclink\eclink-iot-tcp-server\backend\libs\eclink-iot.jar!\com\eclink\iot\base\SocketClientBase.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */